# 在这里定义一下一个Todo的结构
import utils


class Todo:
    class Type:
        SIMPLE = 0  # 简单TODO，没有时间限制
        DAY_TIME = 1  # 今日完成
        START_STOP_TIME = 2  # 有开始结束时间

    def __init__(self, name, type_=Type.SIMPLE, description='', time_start=None, time_stop=None):
        self.tid = None
        self.name = name
        # 尽量不要和builtin重名
        self.type = type_
        self.description = description
        self.finished = False
        # Unix时间戳，None表示未设置
        self.time_start = time_start
        self.time_stop = time_stop
        self.time_finish = None

    def finish(self):
        self.finished = True

    def to_json(self) -> dict:
        return {
            'tid': self.tid,
            'name': self.name,
            'type': self.type,
            'description': self.description,
            'finished': self.finished,
            'time_finish': self.time_finish,
            'time_start': self.time_start,
            'time_stop': self.time_stop
        }

    def from_json(self, js: dict):
        self.tid = utils.get_default(js, 'tid', None)
        self.name = utils.get_default(js, 'name', 'Todo')
        self.type = utils.get_default(js, 'type', Todo.Type.SIMPLE)
        self.description = utils.get_default(js, 'description', '')
        self.finished = utils.get_default(js, 'finished', False)
        self.time_start = utils.get_default(js, 'time_start')
        self.time_stop = utils.get_default(js, 'time_stop')
        self.time_finish = utils.get_default(js, 'time_finish')
        return self

    # 静态方法：从dict格式生成Todo
    @staticmethod
    def from_json_static(js: dict):
        return Todo('Todo').from_json(js)

    # 设置这个函数可以使用str(t o d o)的类型转换
    def __str__(self) -> str:
        return '''===================
tid:\t\t\t%s
name:\t\t\t%s
desp:\t\t\t%s
is_finished:\t%s
===================''' % (self.tid, self.name, self.description, self.finished)
